package top.lrshuai.googlecheck.config;


import com.beust.jcommander.internal.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {
    @Bean
    public Docket ProductApi() {
        return new Docket(DocumentationType.OAS_30)
                .globalRequestParameters(Lists.newArrayList(
                        new RequestParameterBuilder()
                                .name("token")
                                .description("")
                                .parameterIndex(-1)
                                .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("")
                                        .allowEmptyValue(true))
                                .in(ParameterType.HEADER)
                                .build()
                ))
                .apiInfo(
                        new ApiInfoBuilder()
                                .title("接口文档")
                                .version("1.0.0")
                                .description("")
                                .build()
                )
                .groupName("v1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("top.lrshuai"))
                .build();
    }

    /**
     * 防止@EnableMvc把默认的静态资源路径覆盖了，手动设置的方式
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:" + "/swagger-ui/index.html");
    }
}
